package com.open.capacity.reimburse.service.impl;

import com.open.capacity.model.system.SysUser;
import com.open.capacity.reimburse.entity.StdMetting;
import com.open.capacity.reimburse.entity.StdReception;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.DateUtil;
import com.open.capacity.utils.StringUtil;
import com.open.capacity.utils.SysUserUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reimburse.entity.StdTraining;
import com.open.capacity.reimburse.dao.StdTrainingDao;
import com.open.capacity.reimburse.service.StdTrainingService;


@Service
public class StdTrainingServiceImpl implements StdTrainingService {

    @Autowired
    private StdTrainingDao stdTrainingDao;

    /**
     * 添加
     *
     * @param stdTraining
     */
    public int save(StdTraining stdTraining) throws Exception {
        SysUser loginUser = SysUserUtil.getLoginAppUser();

        validition(stdTraining);

        stdTraining.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        stdTraining.setUpdateUserid(loginUser.getId());
        stdTraining.setUpdateUsername(loginUser.getRealName());
        stdTraining.setUpdateTime(new Date());
        return stdTrainingDao.save(stdTraining);
    }

    private void validition(StdTraining stdTraining) throws Exception {
        StdTraining exists = stdTrainingDao.findByCategory(stdTraining.getCategory());
        if (exists != null && !exists.getId().equals(stdTraining.getId())) {
            throw new Exception("不允许同一个培训类别配置多次");
        }
    }

    /**
     * 修改
     *
     * @param stdTraining
     */
    public int update(StdTraining stdTraining) throws Exception {

        validition(stdTraining);

        stdTraining.setUpdateTime(new Date());
        return stdTrainingDao.update(stdTraining);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return stdTrainingDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public StdTraining findById(String id) {
        return stdTrainingDao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<StdTraining> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<StdTraining> list = stdTrainingDao.findAll(params);
        PageInfo<StdTraining> pageInfo = new PageInfo(list);

        return PageResult.<StdTraining>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<StdTraining> findAll(Map<String, Object> params) {
        return stdTrainingDao.findAll(params);
    }

    @Override
    public String verify(String userId, Integer attendCount, Integer workerCount, Date startDate, Date endDate, BigDecimal money) {
        String error_msg = "";

        int days = DateUtil.getBetweenDay(startDate, endDate);
        if (days <= 0) {
            days = 1;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("category", 2);
        List<StdTraining> list = stdTrainingDao.findAll(params);

        boolean flag;
        if (CollectionUtils.isNotEmpty(list)) {
            StdTraining item = list.get(0);
            BigDecimal standard = BigDecimal.ZERO;
            if (days <= item.getOverday()) {
                standard = item.getTotal().multiply(BigDecimal.valueOf(days)).multiply(BigDecimal.valueOf(attendCount + workerCount));
            } else {
                standard = item.getTotal().multiply(BigDecimal.valueOf(item.getOverday())).multiply(BigDecimal.valueOf(attendCount + workerCount));
                standard = standard.add(item.getTotal().multiply(BigDecimal.valueOf(item.getDiscount()/100)).multiply(BigDecimal.valueOf(days - item.getOverday())).multiply(BigDecimal.valueOf(attendCount + workerCount)));
            }
            flag = money.compareTo(standard) <= 0;
            if (!flag) {
                DecimalFormat decimalFormat = new DecimalFormat("###,###.00");
                error_msg = "<p>⊙ 培训费用超标, 培训费用合计不得超过" + decimalFormat.format(standard) + "元</p>";
            }
        }
        return error_msg;
    }

}
